﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TV4Home.WebInterface.UI.Code;

namespace TV4Home.WebInterface.UI
{
    public partial class About : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public override void UpdateUI()
        {
            try
            {
                if (this.ShouldCheckForUpdates)
                {
                    this.mvUpdateCheck.ActiveViewIndex = CheckForUpdates() ? 1 : 0;
                }
            }
            catch (Exception ex)
            {
                this.mvUpdateCheck.ActiveViewIndex = 2;

                lblError.Text += ex.Message;
                lblError.Visible = true;
            }

            base.UpdateUI();
        }

        /// <summary>
        /// Checks for updates.
        /// </summary>
        /// <returns>True if there is a newer version on the Internet, otherwise false.</returns>
        private bool CheckForUpdates()
        {
            Regex versionNumberRegex = new Regex(@"(?<Major>\d*)\.(?<Minor>\d*)(\.(?<Build>\d*)(\.(?<Revision>\d*))?)?");

            SyndicationFeed projectRssFeed;

            // load the Codeplex Releases RSS feed
            using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create("http://tv4home.codeplex.com/Project/ProjectRss.aspx?ProjectRSSFeed=codeplex%3a%2f%2frelease%2fTV4Home"))
                projectRssFeed = SyndicationFeed.Load(xmlReader);

            if (projectRssFeed == null)
                throw new InvalidOperationException("The update RSS feed couldn't be retrieved.");

            if (projectRssFeed.Items.Count() == 0)
                throw new InvalidOperationException("The update RSS feed is empty.");

            // get the latest item in the feed
            SyndicationItem latestItem = projectRssFeed.Items.First();

            // retrieve version number from feed item title
            Match regExMatch = versionNumberRegex.Match(latestItem.Title.Text);
            Version versionOnInternet = regExMatch.Success ? new Version(regExMatch.Value) : new Version("0.0.0.0");

            // if the version on the Internet is newer than the local version -> show notification
            if (new Version(Utils.Misc.WebsiteVersion) < versionOnInternet)
            {
                this.lblNewerVersion.Text = versionOnInternet.ToString();
                this.lblReleaseNotes.Text = latestItem.Summary.Text;

                return true;
            }

            // version is up to date
            return false;
        }

        protected void btnCheckForUpdates_Click(object sender, EventArgs e)
        {
            this.ShouldCheckForUpdates = true;
            this.btnCheckForUpdates.Visible = false;

            UpdateUI();
        }

        bool ShouldCheckForUpdates { get; set; }
    }
}
